<template>
    <div>
      <h1>会员卡统计</h1>
      <!-- 图表容器 -->
      <div id="card-list-chart" style="width: 100%; height: 400px;"></div>
    </div>
  </template>
  
  <script setup>
  import { onMounted } from 'vue';
  import * as echarts from 'echarts';
  import axios from 'axios';
  
  const renderChart = (data) => {
    console.log('Chart data:', data);  // 确认数据传递正确
    const chartDom = document.getElementById('card-list-chart');
    const myChart = echarts.init(chartDom);

    const option = {
        title: {
            text: '会员卡统计',
            left: 'center'
        },
        tooltip: {
            trigger: 'item'
        },
        legend: {
            orient: 'vertical',
            left: 'left'
        },
        series: [
            {
                name: '会员卡类型',
                type: 'pie',
                radius: '50%',
                data: data,
                emphasis: {
                    itemStyle: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    };

    myChart.setOption(option);
};

  
  // 组件挂载时获取数据并渲染图表
  onMounted(() => {
    axios.get('/api/membercard/cardList')
      .then(response => {
        renderChart(response.data);
      })
      .catch(error => {
        console.error('获取会员卡统计数据失败:', error);
      });
  });


//   onMounted(() => {
//     const testData = [
//         { name: '次卡（无期限）', value: 4 },
//         { name: '次卡（有期限）', value: 3 }
//     ];
//     renderChart(testData);
//     });
  </script>
  
  <style lang="less" scoped>
  #card-list-chart {
    width: 100%;
    height: 400px;
  }
  </style>
